package org.mzy.org.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.mzy.basic.anno.PreAuthorize;
import org.mzy.basic.util.AjaxResult;
import org.mzy.basic.util.LoginContext;
import org.mzy.basic.util.PageList;
import org.mzy.org.domain.Department;
import org.mzy.org.domain.Employee;
import org.mzy.org.query.DepartmentQuery;
import org.mzy.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Package: org.mzy.org.controller
 * @ClassName: DepartmentController
 * @Date:2022/9/30 9:16
 * @Description:
 */

/*
 * @controller和@ResponseBody的结合
 * @Controller：表名这是一个Controller类
 * @ResponseBody：以Json字符串的形式返回给客户端
 */
@RestController
@RequestMapping("/dept")
@Api(value = "部门的API", description = "部门相关的CRUD功能")
//@LogAnno
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    /**
     * 添加/修改
     */
    @PutMapping
    @PreAuthorize(name = "添加/修改部门", sn = "department:addOrUpdate")
    public AjaxResult addOrUpdate(@RequestBody Department department) {
        if (null == department.getId()) {
            // 添加
            departmentService.add(department);
        } else {
            // 修改
            departmentService.update(department);
        }
        return AjaxResult.me().success();
    }

    /**
     * 删除 delete localhost:8080/dept/1
     */
    @DeleteMapping("/{id}")
    @PreAuthorize(name = "删除部门", sn = "department:delete")
    public AjaxResult delete(@PathVariable("id") Long id) {
        departmentService.delete(id);
        return AjaxResult.me().success();
    }

    /**
     * 查询所有
     */
    @GetMapping
    @ApiOperation(value = "查询所有部门数据")
    @PreAuthorize(name = "查询所有部门", sn = "department:findAll")
    public List<Department> findAll() {
        return departmentService.findAll();
    }

    /**
     * 根据id查询一个
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询")
    @PreAuthorize(name = "根据id查询部门", sn = "department:findById")
    public Department findById(@ApiParam(value = "ID属性", required = true) @PathVariable("id") Long id) {
        return departmentService.findById(id);
    }

    /**
     * 根据id查询员工集合
     *
     * @return
     */
    @GetMapping("/emp/{id}")
    @ApiOperation(value = "通过ID查询")
    @PreAuthorize(name = "根据id查询员工集合", sn = "department:findEmpById")
    public List<Employee> findEmpById(@ApiParam(value = "ID属性", required = true) @PathVariable("id") Long id) {
//        System.out.println(departmentService.findEmpById(id));
        return departmentService.findEmpById(id);
    }

    /**
     * 分页查询
     */
    @PostMapping
    @PreAuthorize(name = "分页查询部门", sn = "department:queryByPage")
//    @LogAnno(method = "queryByPage",methodDes = "分页查询部门")
    public PageList<Department> queryByPage(@RequestBody DepartmentQuery query, HttpServletRequest request) {
        return departmentService.queryByPage(query);
    }

    @PatchMapping
    @PreAuthorize(name = "批量删除部门", sn = "department:batchDel")
    public AjaxResult batchDel(@RequestBody List<Long> ids) {
        departmentService.batchDel(ids);
        return AjaxResult.me().success();
    }

    @PostMapping("/tree")
    @PreAuthorize(name = "查询部门树", sn = "department:tree")
    public List<Department> deptTree() {
        return departmentService.deptTree();
    }
}
